package com.qfedu.controller;

import com.qfedu.entity.ChartList;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.Student;
import com.qfedu.entity.User;
import com.qfedu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class StudentController {
    /*
    *   fengle
    * 查询学生
    * */
    @Autowired
    private StudentService studentService;
    @RequestMapping("/findStudentByUno")
    public ResponseData findStudentByUno( HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        Student student = studentService.findStudentByUno(user.getNo());
        ResponseData responseData = new ResponseData();
        responseData.setCode(1);
        responseData.setData(student);
        return responseData;
        }
     /*
     * fengle
     * 显示学生名字
     * */
    @RequestMapping("/showStudentName")
    public ResponseData showStudentName(HttpServletRequest request){
        HttpSession session = request.getSession();
        User  user = (User) session.getAttribute("loginUser");
        Student student = studentService.findStudentByUno(user.getNo());

        ResponseData responseData = new ResponseData();
        responseData.setData(student.getName());
        responseData.setCode(1);
        return responseData;
    }
    @RequestMapping("/addStudent")
    public ResponseData addStudent(Student student){
        ResponseData responseData =new ResponseData();
        System.out.println(student);
        Date date = new Date(System.currentTimeMillis());
        student.setEntrytime(date);
        int i = studentService.addStudent(student);
        if(i>0){
            responseData.setMsg(ResponseData.SUCCESS_MSG);
            responseData.setCode(ResponseData.SUCCESS_CODE);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    //显示我的同学
    @RequestMapping("/ShowMyStudent")
    public ResponseData ShowMyStudent(int page,int limit){
        return studentService.findAllVip(page, limit);
    }
    //饼状图
    @RequestMapping("/getStudentBySex")
    public ResponseData getStudentBySex(){
        ChartList chartList = studentService.getStudentBysex();
        ResponseData responseData = new ResponseData();
        responseData.setData(chartList);
        responseData.setCode(ResponseData.SUCCESS_CODE);
        return responseData;
    }
}

